package com.xiaoshuidi.cloud.module.bpm.framework.bpm.core;

import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.bus.BusBridge;
import org.springframework.cloud.bus.BusConstants;
import org.springframework.cloud.bus.BusProperties;
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.support.MessageBuilder;

@Slf4j
public class BpmBusBridge implements BusBridge {
    private final StreamBridge streamBridge;
    private final BusProperties properties;
    @Value("${spring.cloud.nacos.discovery.group:DEFAULT_GROUP}")
    private String tag;

    public BpmBusBridge(StreamBridge streamBridge, BusProperties properties) {
        this.streamBridge = streamBridge;
        this.properties = properties;
    }
    @Override
    public void send(RemoteApplicationEvent event) {
        log.info("发送Bus消息 {}", JsonUtils.toJsonString(event));
        this.streamBridge.send(BusConstants.OUTPUT, MessageBuilder.withPayload(event)
                .setHeader("TAGS", tag)
                .build());
    }
}
